package com.bupt.liutong.admin.core.bo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.transaction.annotation.Transactional;

import com.bupt.liutong.admin.core.base.BaseBo;
import com.bupt.liutong.admin.core.dao.RoleDao;
import com.bupt.liutong.admin.core.dto.Role;
import com.bupt.liutong.admin.core.form.RoleForm;
import com.bupt.liutong.admin.core.form.UserForm;
import com.bupt.liutong.util.BeanUtils;

public class RoleBo extends BaseBo {

	private RoleDao roleDao;
	
	public List<RoleForm> getRolesByPage(RoleForm roleForm, HttpServletRequest request) {
		Role role = new Role();
		sm.pagingWithNoCriteria(roleForm, role, "core_role.getRolesCount");
		List<Role> roleList = roleDao.getRolesByPage(role);
		List<RoleForm> roleFormList = BeanUtils.dtoToForm(roleList,
				RoleForm.class);
		return roleFormList;
	}
	
	public RoleForm getRoleById(RoleForm roleForm) {
		RoleForm resultForm = new RoleForm();
		BeanUtils.copyProperties(resultForm, roleDao.getRoleById(roleForm.getId()));
		return resultForm;
	}
	
	public Map<Integer, Boolean> getRoleModulesAndOpersByRoleId(RoleForm roleForm){
		List<Role> modules = roleDao.getRoleModulesByRoleId(roleForm.getId());
		List<Role> opers = roleDao.getRoleOpersByRoleId(roleForm.getId());
		Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
		for (Role module : modules) {
			map.put(module.getModuleId(), true);
		}
		for (Role oper : opers) {
			map.put(oper.getOperId(), true);
		}
		return map;
	}
	
	@Transactional
	public void insertRole(RoleForm roleForm) {
		Role role = new Role();
		BeanUtils.copyProperties(role, roleForm);
		roleDao.insertRole(role);
	}

	@Transactional
	public int updateRole(RoleForm roleForm) {
		Role role = new Role();
		BeanUtils.copyProperties(role, roleForm);
		return roleDao.updateRole(role);
	}

	@Transactional
	public void updateRoleModule(RoleForm roleForm, String[] moduleIds){
		roleDao.deleteRoleModuleByRoleId(roleForm.getId());
		if (moduleIds != null) {
			for (String moduleId : moduleIds) {
				Role role = new Role();
				role.setId(roleForm.getId());
				role.setModuleId(Integer.parseInt(moduleId));
				roleDao.insertRoleModule(role);
			}
		}
	}

	@Transactional
	public void updateRoleOper(RoleForm roleForm, String[] operIds){
		roleDao.deleteRoleOperByRoleId(roleForm.getId());
		if (operIds != null) {
			for (String operId : operIds) {
				Role role = new Role();
				role.setId(roleForm.getId());
				role.setOperId(Integer.parseInt(operId));
				roleDao.insertRoleOper(role);
			}
		}
	}

	@Transactional
	public int deleteRoles(String[] ids) {
		int rowsAffected = 0;
		for (int i = 0; i < ids.length; i++) {
			rowsAffected += roleDao.deleteRole(Integer.parseInt(ids[i]));
		}
		return rowsAffected;
	}
	
	public void putRoleInfo(HttpSession session, UserForm userForm){
		session.setAttribute("roleIds", roleDao.getRoleIdsByUserName(userForm.getUserName()));
	}
	
	public void removeRoleInfo(HttpSession session){
		session.removeAttribute("roleIds");
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
}

